<?php

namespace App\Console\Commands;

use App\Components\getUsersWeiXinInfo;
use App\Models\Student;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SendMessages extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'check:send';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查考试信息把当日进行的考试提醒参考人员';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $url = 'http://platform.tr.daxuefun.cn/weiui#/examQuery/';
        $today = strtotime(date('Y-m-d', time()));
        $num = 0;
        $info = new getUsersWeiXinInfo();
        $end = $today + 24 * 60 * 60;
        $query = \DB::table('exams')->whereBetween('begin', [$today, $end]);
        $exams = $query->get();
        if ($exams) {
            foreach ($exams as $item) {
                $students = \DB::table('student_exams')->join('students', function ($join) {
                    $join->on('student_exams.s_id', '=', 'students.id');
                })->join('exams', function ($join) {
                    $join->on('student_exams.e_id', '=', 'exams.id');
                })->where('student_exams.e_Id', $item->id)->whereNotNull('students.wx_key')->get();
                if (!empty($students)) {
                    foreach ($students as $item) {

                        if ($item->wx_key){
                            $info->endTMessage($item->wx_key,$url . $item->wx_key,$item->e_name . ' 考试通知',$item->u_name,date('Y-m-d H:i:s', $item->begin),$item->place,'请准时参加考试');
                        $num++;

                        }

                    }

                }
            }
        }
        echo "发送成功，一共有" . $num . "人今日需要参加考试";
    }
}
